package com.sgb.member.weight.threefliter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/*
 * 三级筛选
 * 仅适用三级联动数据列表
 * 仅适用单选模式
 * 初始位置内容自定义设置
 * 勾选模式可通过方法设置
 * 返回数据为PickResultEntity 封装每层所选数据
 * 重置按钮恢复至初始状态并且回调重置方法返给用户处理(自动关闭window)
 * 待完善
 * */
public class MThreeLevelFilterWindow extends BasePopWindow {

    private MPickFilterWindowAdapter pickAdapter1;
    private MPickFilterWindowAdapter pickAdapter2;
    private MPickFilterWindowAdapter pickAdapter3;
    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private RecyclerView recycler3;
    private CommonPopEntity pickResultEntity1;
    private CommonPopEntity pickResultEntity2;
    private CommonPopEntity pickResultEntity3;

    private String all = "全部"; //初始位置显示内容---全部/不限

    private List<CommonPopEntity> list;
    private int currentPosition = 0;

    public MThreeLevelFilterWindow(Context context) {
        super(context);
    }

    public MThreeLevelFilterWindow(Context context, String all) {
        super(context);
        this.all = all;
    }

    @Override
    protected int popLayout() {
        return R.layout.m_filter_packers_3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        recycler3 = holder.getPop().findViewById(R.id.rlv3_list);
        TextView reset = holder.getPop().findViewById(R.id.reset);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);

        //重置
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickResultEntity1 = new CommonPopEntity();
                pickResultEntity2 = new CommonPopEntity();
                pickResultEntity3 = new CommonPopEntity();
                pickAdapter1.setDefItem(0);
                pickAdapter2.setDefItem(0);
                pickAdapter3.setDefItem(0);
                recycler1.scrollToPosition(0);
                recycler2.setVisibility(View.GONE);
                recycler3.setVisibility(View.GONE);
                if (onResultClick != null) {
                    onResultClick.reset();
                }
                dissMiss();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(pickResultEntity1, pickResultEntity2, pickResultEntity3);
                    dissMiss();
                }
            }
        });

        if (pickAdapter1 == null) {
            pickAdapter1 = new MPickFilterWindowAdapter(mContext, R.layout.item_m_filter_child_window, null);
            pickAdapter1.setIsDisplay(false); //勾选隐藏
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new MPickFilterWindowAdapter(mContext, R.layout.item_m_filter_child_window_1, null);
            pickAdapter2.setIsDisplay(true);
            pickAdapter2.setItemSelect(true);
            pickAdapter2.setDefItem(0);
        }
        if (pickAdapter3 == null) {
            pickAdapter3 = new MPickFilterWindowAdapter(mContext, R.layout.item_m_filter_child_window_1, null);
            pickAdapter3.setIsDisplay(true); //勾选显示
            pickAdapter3.setItemSelect(true);
            pickAdapter3.setDefItem(0);
        }

        MLinearLayoutManager manager = new MLinearLayoutManager(mContext);
        recycler1.setLayoutManager(manager);
        recycler1.setAdapter(pickAdapter1);
        MLinearLayoutManager manager1 = new MLinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(pickAdapter2);
        MLinearLayoutManager manager2 = new MLinearLayoutManager(mContext);
        recycler3.setLayoutManager(manager2);
        recycler3.setAdapter(pickAdapter3);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                entity.setCurPos(position);
                currentPosition = position;
                pickResultEntity1 = entity;
                pickResultEntity2 = new CommonPopEntity();
                pickResultEntity3 = new CommonPopEntity();
                recycler3.setVisibility(View.GONE);
                if (position == 0) {
                    recycler2.setVisibility(View.GONE);
                    return;
                }
                setData2(entity.getChildren(), 0);
            }
        });

        pickAdapter2.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                entity.setCurPos(position);
                pickResultEntity2 = entity;
                if (!isShowThree) {
                    return;
                }
                pickResultEntity3 = new CommonPopEntity();
                if (position == 0) {
                    recycler3.setVisibility(View.GONE);
                    return;
                }
                setData3(entity.getChildren(), 0);
                recycler3.setVisibility(View.VISIBLE);
            }
        });

        pickAdapter3.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                entity.setCurPos(position);
                pickResultEntity3 = entity;
            }
        });

        pickResultEntity1 = new CommonPopEntity();
        pickResultEntity2 = new CommonPopEntity();
        pickResultEntity3 = new CommonPopEntity();
        recycler1.setVisibility(View.VISIBLE);
        recycler2.setVisibility(View.GONE);
        recycler3.setVisibility(View.GONE);
    }

    private boolean isShowThree = true;

    public void setThreeLeveGone(boolean b) {
        this.isShowThree = b;
    }

    public void setData(List<CommonPopEntity> listData, int index) {
        this.list = listData;
        if (listData == null || listData.size() == 0) {
            return;
        }
        if (!list.get(0).getTypeName().equals("全部") && !list.get(0).getTypeName().equals("不限")) {
            list.add(0, new CommonPopEntity("", "全部"));
        }
        pickAdapter1.setDatas(list);
        pickAdapter1.setDefItem(index);
        recycler1.scrollToPosition(index);
    }

    private void setData2(List<CommonPopEntity> list, int index) {
        if (list == null || list.size() == 0) {
            pickAdapter2.setDatas(new ArrayList<>());
            recycler2.setVisibility(View.GONE);
        } else {
            if (!list.get(0).getTypeName().equals("全部") && !list.get(0).getTypeName().equals("不限")) {
                list.add(0, new CommonPopEntity("", "全部"));
            }
            pickAdapter2.setDatas(list);
            recycler2.setVisibility(View.VISIBLE);
        }
        pickAdapter2.setDefItem(index);
        recycler2.scrollToPosition(index);
    }

    private void setData3(List<CommonPopEntity> list, int index) {
        if (list == null || list.size() == 0) {
            pickAdapter3.setDatas(new ArrayList<>());
            recycler3.setVisibility(View.GONE);
        } else {
            if (!list.get(0).getTypeName().equals("全部") && !list.get(0).getTypeName().equals("不限")) {
                list.add(0, new CommonPopEntity("", "全部"));
            }
            pickAdapter3.setDatas(list);
            recycler3.setVisibility(View.VISIBLE);
        }
        pickAdapter3.setDefItem(index);
        recycler3.scrollToPosition(index);
    }

    public void setDefaultItem(int oneIndex, int twoIndex, int threeIndex) {
        this.currentPosition = oneIndex;
        if (list == null) {
            return;
        }
        setData(list, oneIndex);
        List<CommonPopEntity> twoData = list.get(oneIndex).getChildren();
        if (twoData != null && twoData.size() > 0) {
            setData2(twoData, twoIndex);
            List<CommonPopEntity> threeData = list.get(oneIndex).getChildren().get(twoIndex).getChildren();
            if (threeData != null && threeData.size() > 0) {
                setData3(threeData, threeIndex);
            }
        }
    }

    private OnResultClick onResultClick;

    public interface OnResultClick {

        void result(Object key1, Object key2, Object key3);

        void reset();
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }
}
